package com.ronglian.register_mini_program.controller;

import com.ronglian.register_mini_program.model.RegisterOrder;
import com.ronglian.register_mini_program.model.RegisterUser;
import com.ronglian.register_mini_program.model.UserForm;
import com.ronglian.register_mini_program.model.web.ActivityDetailBody;
import com.ronglian.register_mini_program.model.web.RegisterBody;
import com.ronglian.register_mini_program.result.ResultBody;
import com.ronglian.register_mini_program.service.ActivityService;
import com.ronglian.register_mini_program.service.OrderService;
import com.ronglian.register_mini_program.service.RegisterService;
import com.ronglian.register_mini_program.service.UserService;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/meeting")
@Api(tags = "参加活动管理", description = "参加活动管理")
@ApiResponses(value = {
        @ApiResponse(code = 200, message = "Successful — 请求已完成"),
        @ApiResponse(code = 400, message = "请求中有语法问题，或不能满足请求"),
        @ApiResponse(code = 401, message = "未授权客户机访问数据"),
        @ApiResponse(code = 404, message = "服务器找不到给定的资源；文档不存在"),
        @ApiResponse(code = 500, message = "服务器不能完成请求")}
)
public class RegisterController {
    @Autowired
    RegisterService registerService;
    @Autowired
    ActivityService activityService;
    @Autowired
    UserService userService;
    @Autowired
    OrderService orderService;


    @ApiOperation(value = "用户报名活动", notes = "添加报名信息")
    @ApiImplicitParam(paramType = "body", dataType = "RegisterBody", name = "param", value = "信息参数", required = true)
    @RequestMapping(value = "/activityRegister", method = RequestMethod.POST)
    public ResultBody activityRegister(@RequestBody RegisterBody registerBody) {

        try {

            if (registerBody != null) {
                RegisterOrder order = orderService.findOrderWithOpenIdAndActivityId(registerBody.getOpen_id(), registerBody.getActivity_id());
                if (order != null) {
                    return new ResultBody("500", "请勿重复报名!");
                }

                registerService.insertOrder(registerBody);

                RegisterUser user = userService.findByOpenId(registerBody.getOpen_id());
                if (user != null) {
                    if (registerBody.getAge() != null) {
                        user.setAge(registerBody.getAge());
                    }

                    if (!StringUtils.isBlank(registerBody.getCity())) {
                        user.setCity(registerBody.getCity());
                    }
                    if (!StringUtils.isBlank(registerBody.getClass_grade())) {
                        user.setClass_grade(registerBody.getClass_grade());
                    }
                    if (!StringUtils.isBlank(registerBody.getClass_grade())) {
                        user.setClass_grade(registerBody.getClass_grade());
                    }
                    if (!StringUtils.isBlank(registerBody.getCompany())) {
                        user.setCompany(registerBody.getCompany());
                    }
                    if (!StringUtils.isBlank(registerBody.getDepartment())) {
                        user.setDepartment(registerBody.getDepartment());
                    }
                    if (!StringUtils.isBlank(registerBody.getEmail())) {
                        user.setEmail(registerBody.getEmail());
                    }
                    if (!StringUtils.isBlank(registerBody.getHead_image_url())) {
                        user.setHead_image_url(registerBody.getHead_image_url());
                    }
                    if (!StringUtils.isBlank(registerBody.getHome_address())) {
                        user.setHome_address(registerBody.getHome_address());
                    }
                    if (!StringUtils.isBlank(registerBody.getJob_id())) {
                        user.setJob_id(registerBody.getJob_id());
                    }
                    if (!StringUtils.isBlank(registerBody.getMark())) {
                        user.setMark(registerBody.getMark());
                    }
                    if (StringUtils.isNotBlank(registerBody.getArrange())) {
                        user.setArrange(registerBody.getArrange());
                    }
                    if (!StringUtils.isBlank(registerBody.getName())) {
                        user.setName(registerBody.getName());
                    }
                    if (!StringUtils.isBlank(registerBody.getNickname())) {
                        user.setNickname(registerBody.getNickname());
                    }
                    if (!StringUtils.isBlank(registerBody.getOpen_id())) {
                        user.setOpen_id(registerBody.getOpen_id());
                    }
                    if (!StringUtils.isBlank(registerBody.getProvince())) {
                        user.setProvince(registerBody.getProvince());
                    }
                    if (!StringUtils.isBlank(registerBody.getQq_number())) {
                        user.setQq_number(registerBody.getQq_number());
                    }
                    if (!StringUtils.isBlank(registerBody.getSchool())) {
                        user.setSchool(registerBody.getSchool());
                    }
                    if (registerBody.getSex() != null) {
                        user.setSex(registerBody.getSex());
                    }
                    if (!StringUtils.isBlank(registerBody.getStudent_id())) {
                        user.setStudent_id(registerBody.getStudent_id());
                    }
                    if (!StringUtils.isBlank(registerBody.getTelephone())) {
                        user.setTelephone(registerBody.getTelephone());
                    }
                    if (!StringUtils.isBlank(registerBody.getWechat_number())) {
                        user.setWechat_number(registerBody.getWechat_number());
                    }
                    if (!StringUtils.isBlank(registerBody.getWork_position())) {
                        user.setWork_position(registerBody.getWork_position());
                    }
                    userService.update(user);
                    System.out.println("用户注册成功！：" + user.getName() + ":" + user.getSex() + user.getNickname());
                }
                ActivityDetailBody detailBody = activityService.findByPrimaryKey(registerBody.getActivity_id());
                if (detailBody != null) {
                    return new ResultBody(detailBody);
                }
            }

            return new ResultBody("500", "请提交信息！");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultBody("500", "用户报名活动失败！");
        }


    }


    @ApiOperation(value = "取消报名", notes = "取消报名")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "open_id", value = "微信唯一标识id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "activity_id", value = "活动id", required = true)
    })
    @RequestMapping(value = "/cancelRegister", method = RequestMethod.POST)
    public ResultBody cancelRegister(@RequestParam("open_id") String open_id, @RequestParam("activity_id") Integer activity_id) {

        try {
            registerService.deleteOrder(open_id, activity_id);
            return new ResultBody();
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultBody("500", "取消报名失败！");
        }

    }

    @ApiOperation(value = "用户是否已经报名", notes = "用户是否已经报名")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "open_id", value = "微信唯一标识id", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "activity_id", value = "活动id", required = true)
    })
    @RequestMapping(value = "/isRegister", method = RequestMethod.POST)
    public ResultBody isRegister(@RequestParam("open_id") String open_id, @RequestParam("activity_id") Integer activity_id) {
        try {

            RegisterOrder order = orderService.findOrderWithOpenIdAndActivityId(open_id, activity_id);
            if (order != null) {
                return new ResultBody(order);
            }
            return new ResultBody("500", "用户未报名");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultBody("500", "用户是否已经报名失败！");
        }

    }


    @ApiOperation(value = "openid修改用户信息", notes = "微信小程序获取openid")
    @ApiImplicitParam(paramType = "body", dataType = "RegisterUser", name = "注册用户信息", value = "注册用户信息", required = true)
    @RequestMapping(value = "/updateUserInfo", method = RequestMethod.POST)
    public ResultBody updateUserInfo(@RequestBody RegisterUser registerUser) {

        try {

            //查询是否拥有此用户
            String openid = registerUser.getOpen_id();
            if (openid == null) {
                return new ResultBody("500", "openid为空");
            }
            RegisterUser user = userService.findByOpenId(openid);
            RegisterUser newUSer = null;
            if (user == null) {
                userService.insert(registerUser);
                newUSer = userService.findByOpenId(openid);
                System.out.println("add" + registerUser.getOpen_id() + ":" + registerUser.getHead_image_url());
            } else {
                userService.update(registerUser);
                newUSer = userService.findByOpenId(openid);
                System.out.println("update:" + registerUser.getOpen_id() + ":" + registerUser.getHead_image_url());
            }
            return new ResultBody(newUSer);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultBody("500", "openid修改用户信息失败！");
        }

    }

    @ApiOperation(value = "查询所有订单", notes = "查询所有订单")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "page_num", value = "页码", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "page_size", value = "页面数据的数量", required = true)
    })
    @RequestMapping(value = "/selectAllOrder", method = RequestMethod.GET)
    public ResultBody selectAllOrder(
            @RequestParam("page_num") int page_num, @RequestParam("page_size") int page_size
    ) {
        try {
            List<RegisterOrder> registerOrderList = orderService.findAll(page_num, page_size);
            if (registerOrderList != null && registerOrderList.size() > 0) {
                return new ResultBody(registerOrderList);
            }
            return new ResultBody("500", "没有订单信息！");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultBody("500", "openid修改用户信息失败！");
        }

    }


    @ApiOperation(value = "存储发送模板的form_id", notes = "存储发送模板的form_id")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "open_id", value = "微信用户唯一标识", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "form_id", value = "表单id", required = true)
    })
    @RequestMapping(value = "/addFormId", method = RequestMethod.POST)
    public ResultBody addFormId(
            @RequestParam("open_id") String open_id, @RequestParam("form_id") String form_id
    ) {
        try {
            if (StringUtils.isBlank(open_id) || StringUtils.isBlank(form_id)) {
                return new ResultBody("500","open_id或form_id不能为空！");
            }
            UserForm form = new UserForm();
            long current_time = System.currentTimeMillis();
            form.setCreate_time(current_time);
            form.setUpdate_time(current_time);
            form.setExpired_in(604800000);
            form.setIs_used(0);
            form.setOpen_id(open_id);
            form.setForm_id(form_id);
            userService.addFormId(form);
            return new ResultBody(form);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultBody("500", "存储表单id失败");
        }

    }


}
